import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { Form, Input, Button, Card, message } from 'antd';
import { UserOutlined, LockOutlined } from '@ant-design/icons';
import api from '../utils/api';

export default function Login() {
  const [loading, setLoading] = useState(false);
  const navigate = useNavigate();

  const onFinish = async (values) => {
    setLoading(true);
    try {
      const res = await api.post('/auth/login', values);
      if (res.success) {
        localStorage.setItem('token', res.data.token);
        localStorage.setItem('user', JSON.stringify(res.data.user));
        message.success('登录成功');
        navigate('/');
      } else {
        message.error(res.message || '登录失败');
      }
    } catch (error) {
      message.error(error.response?.data?.message || '登录失败');
    } finally {
      setLoading(false);
    }
  };

  return (
    <div style={{
      minHeight: '100vh',
      display: 'flex',
      alignItems: 'center',
      justifyContent: 'center',
      background: 'linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%)'
    }}>
      <Card
        style={{
          width: 400,
          background: 'rgba(0, 0, 0, 0.7)',
          backdropFilter: 'blur(10px)',
          border: '1px solid rgba(212, 175, 55, 0.3)',
          borderRadius: 16,
          boxShadow: '0 8px 32px rgba(212, 175, 55, 0.15)'
        }}
        bodyStyle={{ padding: 40 }}
      >
        <h2 style={{
          textAlign: 'center',
          marginBottom: 32,
          color: '#d4af37',
          fontSize: 28,
          fontWeight: 600,
          textShadow: '0 2px 4px rgba(212, 175, 55, 0.3)'
        }}>
          🍷 酒吧会员系统
        </h2>

        <Form
          name="login"
          onFinish={onFinish}
          size="large"
        >
          <Form.Item
            name="phone"
            rules={[
              { required: true, message: '请输入手机号' },
              { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号' }
            ]}
          >
            <Input
              prefix={<UserOutlined style={{ color: '#d4af37' }} />}
              placeholder="手机号"
              style={{
                background: 'rgba(255, 255, 255, 0.05)',
                border: '1px solid rgba(212, 175, 55, 0.2)',
                color: '#fff'
              }}
            />
          </Form.Item>

          <Form.Item
            name="password"
            rules={[{ required: true, message: '请输入密码' }]}
          >
            <Input.Password
              prefix={<LockOutlined style={{ color: '#d4af37' }} />}
              placeholder="密码"
              style={{
                background: 'rgba(255, 255, 255, 0.05)',
                border: '1px solid rgba(212, 175, 55, 0.2)',
                color: '#fff'
              }}
            />
          </Form.Item>

          <Form.Item style={{ marginBottom: 0 }}>
            <Button
              type="primary"
              htmlType="submit"
              loading={loading}
              block
              style={{
                background: 'linear-gradient(135deg, #d4af37 0%, #f4e5a1 100%)',
                border: 'none',
                height: 48,
                fontSize: 16,
                fontWeight: 600,
                color: '#1a1a1a',
                boxShadow: '0 4px 12px rgba(212, 175, 55, 0.3)'
              }}
            >
              登 录
            </Button>
          </Form.Item>
        </Form>

        <div style={{
          marginTop: 24,
          textAlign: 'center',
          color: 'rgba(255, 255, 255, 0.5)',
          fontSize: 14
        }}>
          没有账号？
          <a
            onClick={() => navigate('/register')}
            style={{
              color: '#d4af37',
              marginLeft: 8,
              cursor: 'pointer'
            }}
          >
            立即注册
          </a>
        </div>
      </Card>
    </div>
  );
}
